package com.duowei.dw_pos.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.Toast;

import com.duowei.dw_pos.R;
import com.duowei.dw_pos.adapter.SpacesItemDecoration;
import com.duowei.dw_pos.bean.DMFKFS;
import com.duowei.dw_pos.bean.Moneys;
import com.duowei.dw_pos.dialog.MsgInputDialog;

import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class OtherPayFragment extends AppCompatDialogFragment{
    public OtherPayFragment() {
        // Required empty public constructor
    }
    public interface OnItemClickListener{
        void onItemClick(DMFKFS dmfkfs);
    }
    private OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(OnItemClickListener listener){
        mOnItemClickListener=listener;
    }
    public static OtherPayFragment newInstance() {
        Bundle args = new Bundle();
        OtherPayFragment fragment = new OtherPayFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_other_pay, container, false);
        RecyclerView rv = (RecyclerView) inflate.findViewById(R.id.rv);
        rv.addItemDecoration(new SpacesItemDecoration(10));
        rv.setLayoutManager(new GridLayoutManager(getActivity(),3));
        List<DMFKFS> dmfkfsList = DataSupport.where("bm!='BBBBB' and bm!='DDDDD' and fklb!='1' and by2!='1'").find(DMFKFS.class);
        RvAdapter adapter = new RvAdapter(dmfkfsList);
        rv.setAdapter(adapter);
        return inflate;
    }

    class RvAdapter extends RecyclerView.Adapter<RvAdapter.MyViewHold>{
        List<DMFKFS>mList;
        public RvAdapter(List<DMFKFS> list) {
            mList = list;
        }
        @Override
        public MyViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.other_pay_item, null);
            MyViewHold viewHold = new MyViewHold(inflate);
            return viewHold;
        }
        @Override
        public void onBindViewHolder(MyViewHold holder, int position) {
            final DMFKFS dmfkfs = mList.get(position);
            holder.mCtv.setText(dmfkfs.getNr());
            holder.mCtv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String content="1".equals(dmfkfs.getFkfs())?"张数:":"金额:";
                    InputNumDialogFragment fragment = InputNumDialogFragment.newInstance(dmfkfs.getNr(), content);
                    fragment.show(getFragmentManager(),null);
                    fragment.setOnOkBtnClickListener(new InputNumDialogFragment.OnOkBtnClickListener() {
                        @Override
                        public void onOkBtnClick(float inputValue) {
                            float otherMoney;
                            if("1".equals(dmfkfs.getFkfs())){
                                float inputMoney=inputValue*Float.parseFloat(dmfkfs.getDyqmz());
                                if(inputMoney<=Moneys.wfjr){
                                    otherMoney=inputMoney;
                                }else if((inputMoney-Moneys.wfjr)<Float.parseFloat(dmfkfs.getDyqmz())&&inputMoney>Moneys.wfjr){
                                    otherMoney=Moneys.wfjr;
                                }else{
                                    Toast.makeText(getActivity(),"您输入的张数过多",Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                dmfkfs.setDyqzs((int)inputValue);
                            }else{
                                otherMoney=inputValue;
                                if(otherMoney>Moneys.wfjr){
                                    Toast.makeText(getActivity(),"您输入的金额过多",Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                dmfkfs.setDyqzs(0);
                            }
                            dmfkfs.setMoney(otherMoney);
                            mOnItemClickListener.onItemClick(dmfkfs);
                            dismiss();
                        }
                    });
                }
            });
        }
        @Override
        public int getItemCount() {
            return mList.size();
        }
        class MyViewHold extends RecyclerView.ViewHolder{
            public CheckedTextView mCtv;
            public MyViewHold(View itemView) {
                super(itemView);
                mCtv = (CheckedTextView) itemView.findViewById(R.id.ctv_pay);
            }
        }
    }
}
